<?php

namespace App\Http\Controllers\Admin\Posts;

use App\Area;
use App\Service\Category;
use App\Post;
use App\Repositories\Category as CategoryRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
    private $postManager;
    private $categoryManager;
    private $categoryRepository;
    private $request;
    public function __construct (Request $request)
    {
        $this->postManager = new Post();
        $this->categoryManager = new Category();
        $this->categoryRepository = new CategoryRepository();
        $this->request = $request;
    }

    /**
     * 文章列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(){
        if(isset($this->request->act)){
            $act = $this->request->act;
            return $this->{$act}();
        }else{
            $categoriesStr = $this->categoryManager->getByIspart(true,'',0);

            return view('admin.posts.post.index',[
                'status' =>    $this->postManager->getStatusList(),
                'article_attrs'  =>  $this->postManager->getArticle_attrsList(),
                'categories'    =>  $categoriesStr,
                'postManager'   =>  $this->postManager
            ]);
        }

    }

    /**
     * ajax列表
     * @return array
     */
    public function indexAjax(){
        return $this->page_json_render($this->postManager->getPageList($this->request));
    }


    /**
     * 编辑页面
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id){
        $model = $this->postManager->where('id',$id)->with('category')->first();
        $model->article_attrAry = $this->postManager->getArticle_attrs($model->article_attrs);
        $categoriesStr = $this->categoryManager->getByIspart(true,$model->category_id,0);

        $dir = resource_path('views').'/web/posts/view/';
        return view('admin.posts.post.add-edit',[
            'status' =>    $this->postManager->getStatusList(),
            'categories'    =>  $categoriesStr,
            'article_attrs'  =>  $this->postManager->getArticle_attrsList(),
            'areas' =>  Area::all()->toArray(),
            'postManager'   =>  $this->postManager,
            'templates'   =>  getDirList($dir),
            'id'    =>  $id,
            'model' =>  $model
        ]);
    }


    /**
     * 添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(){
        $dir = resource_path('views').'/web/posts/view/';
        $categoriesStr = $this->categoryManager->getByIspart(true,old('category_id'),0);

//        $s = $this->categoryManager->with('children')->where('pid',0)->get();
//        dd($s);
        return view('admin.posts.post.add-edit',[
            'status' =>    $this->postManager->getStatusList(),
            'article_attrs'  =>  $this->postManager->getArticle_attrsList(),
            'categories'    =>  $categoriesStr,
            'areas' =>  Area::all()->toArray(),
            'postManager'   =>  $this->postManager,
            'templates'   =>  getDirList($dir)
        ]);
    }

    /**
     * 保存或插入
     * @return \Illuminate\Http\RedirectResponse
     */
    public function saveOrInsert(){
        //dd($this->request->post());
        if($this->request->post('use_link')){
            $this->validate($this->request,[
                'post_title'    =>  'required|max:50',
                'digest'    =>  'max:255',
                'link'   =>  'url',
                'category_id'   =>  'required',
                //'thumb' =>  'required',
            ]);
        }else{
            $this->validate($this->request,[
                'post_title'    =>  'required|max:50',
                'digest'    =>  'max:255',
                'category_id'   =>  'required',
                //'thumb' =>  'required',
            ]);
        }

        $this->postManager->insertOrUpdate($this->request->post());
        return redirect()->route('admin.posts')->with(
            $this->success()
        );
    }

    /**
     * @return array
     */
    private function draft(){
        try{
            $this->postManager->draft($this->request->id);
            return  ['info'  =>  'success','msg'    =>  $this->request->id];
        }catch (\Exception $e){
            return ['info' =>  'error'];
        }
    }

    /**
     * @return array
     */
    private function published(){
        try{
            $this->postManager->published($this->request->id);
            return  ['info'  =>  'success'];
        }catch (\Exception $e){
            return ['info' =>  'error'];
        }
    }


    /**
     * @return array
     */
    private function delete(){
        try{
            $this->postManager->del($this->request->id);
            return  ['info'  =>  'success'];
        }catch (\Exception $e){
            return ['info' =>  'error'];
        }
    }

    private function sortCol(){
        try{
            $this->postManager->sortCol($this->request->id,$this->request->sort);
            return  ['info'  =>  'success'];
        }catch (\Exception $e){
            return ['info' =>  'error'];
        }
    }
    private function push(){
        //$weChatManager = new WeChat();
        //$return = $weChatManager->uploadArticle($this->request->id);
        return ['info'  =>  'success'];
    }
}
